<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索，添加商品 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入商品名"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表 -->
      <el-table :data="goodsList" style="100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95"
        ></el-table-column>
        <el-table-column
          label="商品重量(g)"
          prop="goods_weight"
          width="95"
        ></el-table-column>
        <el-table-column label="创建时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="125">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              @click="goEditPage(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 保存商品列表信息
      goodsList: null,
      // 总数据条数
      total: 0
    }
  },
  methods: {
    //  根据分页获取对应的商品列表
    async getGoodsList() {
      const { data: res } = await this.https.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('商品列表获取失败!')
      }
      this.$message.success('商品列表获取成功!')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 每页条数切换
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页切换
    handleCurrentChange(newCurrent) {
      this.queryInfo.pagenum = newCurrent
      this.getGoodsList()
    },
    // 删除按钮 点击事件函数  根据id删除对应的参数或属性
    async removeGoods(goodId) {
      //  弹窗提示用户是否要删除
      const result = await this.$messageBox
        .confirm('此操作将永久删除该商品, 是否继续?', '删除提示', {
          confirmButtonText: '确认删除',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .catch((err) => err)
      if (result !== 'confirm') {
        return this.$message.info('取消删除成功!')
      }
      // 确认删除，发送请求
      const { data: res } = await this.https.delete(`goods/${goodId}`)
      if (res.meta.status !== 200) {
        return this.$message.error('商品删除失败!')
      }
      this.$message.success('商品删除成功!')
      this.getGoodsList()
    },
    goAddPage() {
      this.$router.push('/goods/add')
    },
    goEditPage(goodId) {}
  },
  created() {
    this.getGoodsList()
  }
}
</script>

<style lang="less" scoped>
</style>
